package com.le.tester.book.effectivejava3rdchinese.secondReading.l40;

import java.util.HashSet;
import java.util.Set;

/**
 * createTime：2024-09-23 9:34
 * description：始终使用override注解
 */
public class Bigram {

    private final char first;

    private final char second;

    public Bigram(char first, char second) {
        this.first = first;
        this.second = second;
    }

//    @Override
//    public boolean equals(Bigram b) {
//        return b.first == first && b.second == second;
//    }


    //注意你继承的是Object类，继承的参数是需要和父类一致的
    //在new Bigram的时候，执行了equal方法;
    @Override
    public boolean equals(Object obj) {
        if (!(obj instanceof Bigram)) {
            return false;
        }
        Bigram big = (Bigram) obj;
        return big.first == first && big.second == second;
    }

    public int hashCode() {
        return 31 * first + second;
    }

    public static void main(String[] args) {
        //set集合，自动去重
        Set<Bigram> s = new HashSet<>();
        for (int i = 0; i < 10; i++) {
            for (char ch = 'a'; ch <= 'z'; ch++) {
                s.add(new Bigram(ch, ch));
            }
        }
        System.out.println(s.size());
    }
}
